package ca.etsmtl.logti.log619.lab05.filter;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import ca.etsmtl.logti.log619.lab05.utilities.DBListener;

import com.enigmastation.classifier.FisherClassifier;
import com.enigmastation.classifier.WordLister;
import com.enigmastation.classifier.impl.FisherClassifierImpl;

public class FisherFilter implements EmailFilter {

	private FisherClassifier classifier;
	private WordLister wordLister;
	
	public FisherFilter() {
		classifier = new FisherClassifierImpl();
		DBListener listener = new DBListener();
		listener.load(classifier);
		//classifier.addListener(new DBListener());
		wordLister = new WordLister() {
			private static final long serialVersionUID = 1L;

			@Override
			public Set<String> getUniqueWords(Object o) {
				HashSet<String> uniqueWords = new HashSet<String>();
				StringTokenizer tokenizer = new StringTokenizer(o.toString());
				while(tokenizer.hasMoreTokens()) {
					String token = tokenizer.nextToken();

					token = token.replaceAll("[^\\p{L}]", " ");

					String[] words = token.split("\\s+");

					for (String word : words) {
						if(2 < word.length())
							uniqueWords.add(word);
					}
				}
				return uniqueWords;
			}
		};

	}
	
	@Override
	public String getFilterName() {
		return getClass().getSimpleName();
	}

	@Override
	public byte checkSpam(MimeMessage message) {
		try {
			Object messageContent = message.getContent();
			if (messageContent instanceof String) {
				String textMessageContent = (String) messageContent;
				if (textMessageContent != null) {
					String classification = classifier.getClassification(textMessageContent, "unknown");
					if(classification.equals("spam"))
						return 100;
					else
						return 0;
					/*Set<String> contentUniqueWords = wordLister.getUniqueWords(textMessageContent);
					if(type.equals("spam"))
						classifier.train(contentUniqueWords, "spam");
					else
						classifier.train(contentUniqueWords, "ham");*/
				}
			}
			Object messageSubject = message.getSubject();
			if (messageSubject instanceof String) {
				String textMessageSubject = (String) messageSubject;
				if (textMessageSubject != null) {
					String classification = classifier.getClassification(textMessageSubject, "unknown");
					if(classification.equals("spam"))
						return 100;
					else
						return 0;
					/*et<String> subjectUniqueWords = wordLister.getUniqueWords(textMessageSubject);
					if(type.equals("spam"))
						classifier.train(subjectUniqueWords, "spam");
					else
						classifier.train(subjectUniqueWords, "ham");*/
				}
			}
			@SuppressWarnings("unchecked")
			Enumeration<String> headerLines = message.getAllHeaderLines();
            while (headerLines.hasMoreElements()) {
                String headerLine = headerLines.nextElement();
				if (headerLine != null) {
					String classification = classifier.getClassification(headerLine, "unknown");
					if(classification.equals("spam"))
						return 100;
					else
						return 0;
					/*Set<String> subjectUniqueWords = wordLister.getUniqueWords(headerLine);
					if(type.equals("spam"))
						classifier.train(subjectUniqueWords, "spam");
					else
						classifier.train(subjectUniqueWords, "ham");*/
				}				
			}
		} catch (IOException e) {
		} catch (MessagingException e) {
		}

		return 0;
	}

	@Override
	public byte getConfidenceLevel() {
		return 100;
	}

	@Override
	public boolean enabled() {
		return true;
	}

}
